import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:richedit/constant.dart';
import 'package:webview_flutter/webview_flutter.dart';

typedef PPTListener = Function(int current, int total);

class PPTWidget extends StatefulWidget {
  final String pptFilePath;
  final PPTListener? pptListener;

  const PPTWidget({Key? key, required this.pptFilePath, this.pptListener}) : super(key: key);

  @override
  PPTWidgetState createState() => PPTWidgetState();
}

class PPTWidgetState extends State<PPTWidget> {
  bool isTencentCos = false;
  WebViewController? controller;
  late String _previewUrl;

  @override
  void initState() {
    isTencentCos = false;
    if (widget.pptFilePath.contains("aliyuncs")){
      _previewUrl = "${OSS_WEB_OFFICE_URL}${widget.pptFilePath}";
    }else{
      _previewUrl = "${COS_WEB_OFFICE_URL}${widget.pptFilePath}";
    }

    ///移动端暂时不用腾讯的ppt预览
    // if (widget.pptFilePath.contains('aliyuncs')) {
    //   isTencentCos = false;
    //   _status = RequestStatus.success;
    //   _previewUrl = "${URLConstants.WEB_OFFICE_URL}${widget.pptFilePath}";
    // } else {
    //   isTencentCos = true;
    //   getTencentDocPreview();
    // }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned.fill(
          child: WebView(
            javascriptMode: JavascriptMode.unrestricted,
            initialUrl: _previewUrl,
            javascriptChannels: <JavascriptChannel>{
              _messageJavascriptChannel(context),
            },
            onWebViewCreated: (controller) {
              this.controller = controller;
            },
          ),
        ),
        if (isTencentCos)
          Positioned.fill(
              child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  nextAnim();
                },
              ))
      ],
    );
  }

  JavascriptChannel _messageJavascriptChannel(BuildContext context) {
    if (isTencentCos) {
      return JavascriptChannel(
          name: "Tencent",
          onMessageReceived: (message) {
            var json = jsonDecode(message.message);
            if (widget.pptListener != null) {
              widget.pptListener!(json['current'], json['total']);
            }
          });
    }
    return JavascriptChannel(
        name: "Toaster",
        onMessageReceived: (message) {
          var json = jsonDecode(message.message);
          String current = json['page']['idx'];
          String total = json['page']['total'];
          if (widget.pptListener != null) {
            widget.pptListener!(int.tryParse(current) ?? 0, int.tryParse(total) ?? 0);
          }
        });
  }

  void prePage() {
    controller?.evaluateJavascript("prePage()");
  }

  void nextPage() {
    controller?.evaluateJavascript("nextPage()");
  }

  void preAnim() {
    controller?.evaluateJavascript("preAnim()");
  }

  void nextAnim() {
    controller?.evaluateJavascript("nextAnim()");
  }

}
